# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def removeZeroSumSublists(self, head: ListNode) -> ListNode:
        array = []
        preSum = {0: -1}
        node = head
        while node:
            array.append(node.val)
            node = node.next
        arrSum = 0
        i = 0
        for i in range(len(array)):
            arrSum += array[i]
            if arrSum in preSum and array[preSum[arrSum]] is not None:
                j = preSum[arrSum]
                while j < i:
                    array[j + 1] = None
                    j += 1
            else:
                preSum[arrSum] = i
        dummy = ListNode(None)
        node = dummy
        for i in range(len(array)):
            if array[i] is not None:
                node.next = ListNode(array[i])
                node = node.next
        return dummy.next
